﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using ModemGSM;
using PhoneManager.LanguageText;

namespace PhoneManager
{
    public partial class FrmSMS : FrmDefault
    {
        private UCSMS UCSelected = null;
        public FrmSMS()
        {
            InitializeComponent();
        }

        private void BntCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmSMS_Load(object sender, EventArgs e)
        {
            ChkListPhoneBook.Items.Clear();
            foreach (PhoneBook item in SharedLib.PhonesBook)
            {
                ChkListPhoneBook.Items.Add(item);
            }
            ChkListPhoneBook.SelectedIndex = -1;
            ShowInBox();
            ShowUnSent();
            ShowSent();
            TabInOutBox.SelectedIndex = 0;
            BntSendSMS.Enabled = SharedLib.ModemGSM.ModelATCommands.SMSSendMessageCMD;
            BntClear.Enabled = SharedLib.ModemGSM.ModelATCommands.SMSSendMessageCMD;
            TxtNumber.Enabled = SharedLib.ModemGSM.ModelATCommands.SMSSendMessageCMD;
            TxtMsg.Enabled = SharedLib.ModemGSM.ModelATCommands.SMSSendMessageCMD;
            ChkSaveOutBox.Enabled = SharedLib.ModemGSM.ModelATCommands.SMSSendMessageStorageCMD;

            lnkUnPhonebook.Enabled = SharedLib.ModemGSM.ModelATCommands.SMSSendMessageCMD;
            ChkListPhoneBook.Enabled = SharedLib.ModemGSM.ModelATCommands.SMSSendMessageCMD;

            lnkAllBox.Enabled = SharedLib.ModemGSM.ModelATCommands.SMSListMessageCMD;
            lnkUnAllBox.Enabled = SharedLib.ModemGSM.ModelATCommands.SMSListMessageCMD;
            TabInOutBox.Enabled = SharedLib.ModemGSM.ModelATCommands.SMSListMessageCMD;
            BntDelete.Enabled = false;
            BntEdt.Enabled = false;
            BntRead.Enabled = false;
        }
        private string FindName(string Number)
        {
            string Name = "";
            string code = Properties.Settings.Default.DefaultCodeOper.ToUpper().Replace("X", "");
            string NewNum = Number;
            if (NewNum.StartsWith(code))
            {
                NewNum = NewNum.Substring(Properties.Settings.Default.DefaultCodeOper.Length);
            }

            foreach (PhoneBook item in ChkListPhoneBook.Items)
            {
                if (item.Number == Number || item.Number == NewNum)
                {
                    Name = item.Name;
                    break;
                }
            }
            return Name;
        }

        private void UC_EventUCSMSSeleted(object sender, EventArgs e)
        {
            UCSMS UC = (UCSMS)sender;
            if (UC.Seleted)
            {
                BntEdt.Enabled = true;
                BntRead.Enabled = true;
            }
            else
            {
                if (ActiveControl.Name == BntRead.Name)
                {
                    UCSelected = UC;
                    BntEdt.Enabled = true;
                    BntRead.Enabled = true;
                    UC.Seleted = true;
                }
                else if (ActiveControl.Name == BntEdt.Name)
                {
                    UCSelected = UC;
                    BntEdt.Enabled = true;
                    BntRead.Enabled = true;
                    UC.Seleted = true;
                }
                else
                {
                    BntEdt.Enabled = false;
                    BntRead.Enabled = false;
                }
            }
        }

        private void UC_EventUCSMSChecked(object sender, EventArgs e)
        {
            UCSMS UC = (UCSMS)sender;
            if (UC.Checked)
            {
                if (SharedLib.ModemGSM.ModelATCommands.SMSDeleteMessageStorageCMD)
                {
                    BntDelete.Enabled = true;
                }
                else
                {
                    BntDelete.Enabled = false;
                    lnkUnAllBox.Enabled = false;
                    lnkAllBox.Enabled = false;
                }
            }
            else
            {
                Control.ControlCollection CtrlCols = FindCurrentPanel();
                CountCheckedPanels(CtrlCols);
            }
        }

        private void ShowInBox()
        {
            List<SMSStorage> Items = new List<SMSStorage>();
            flowLayoutInBox.Controls.Clear();
            if (SharedLib.ModemGSM.ModelATCommands.SMSListMessageCMD)
            {
                Items = SharedLib.ModemGSM.SMSMessagesRead;
                Items.AddRange(SharedLib.ModemGSM.SMSMessagesUnread.ToArray());
                Items.Sort(new OrderSMSDesc());
                foreach (SMSStorage item in Items)
                {
                    UCSMS UC = new UCSMS();
                    UC.IndexSMS = item.ID;
                    UC.TypeMsg = item.Location;
                    UC.Message = item.Message;
                    UC.Number = item.Number;
                    UC.NamePhonebook = FindName(item.Number);
                    UC.Timezone = item.TimeZone;
                    UC.DateTime = item.DateTime;
                    UC.EventUCSMSChecked += new UCSMS.UCSMSCheckedEventHandler(UC_EventUCSMSChecked);
                    UC.EventUCSMSSeleted += new UCSMS.UCSMSSelectedEventHandler(UC_EventUCSMSSeleted);
                    flowLayoutInBox.Controls.Add(UC);
                }
            }
            TabInBox.Text = ResourcesText.WordInbox + " (" + Items.Count.ToString().PadLeft(3, '0') + ")";
        }
        private void ShowUnSent()
        {
            List<SMSStorage> Items = new List<SMSStorage>();
            flowLayoutOutUnSent.Controls.Clear();
            if (SharedLib.ModemGSM.ModelATCommands.SMSListMessageCMD)
            {
                Items = SharedLib.ModemGSM.SMSMessagesUnSent;
                Items.Sort(new OrderSMSDesc());
                foreach (SMSStorage item in Items)
                {
                    UCSMS UC = new UCSMS();
                    UC.IndexSMS = item.ID;
                    UC.TypeMsg = item.Location;
                    UC.Message = item.Message;
                    UC.Number = item.Number;
                    UC.NamePhonebook = FindName(item.Number);
                    UC.Timezone = item.TimeZone;
                    UC.DateTime = item.DateTime;
                    UC.EventUCSMSChecked += new UCSMS.UCSMSCheckedEventHandler(UC_EventUCSMSChecked);
                    UC.EventUCSMSSeleted += new UCSMS.UCSMSSelectedEventHandler(UC_EventUCSMSSeleted);
                    flowLayoutOutUnSent.Controls.Add(UC);
                }
            }
            TabUnSent.Text = ResourcesText.Unsent +  " (" + Items.Count.ToString().PadLeft(2, '0') + ")";
        }
        private void ShowSent()
        {
            List<SMSStorage> Items = new List<SMSStorage>();
            flowLayoutOutBox.Controls.Clear();
            if (SharedLib.ModemGSM.ModelATCommands.SMSListMessageCMD)
            {
                Items = SharedLib.ModemGSM.SMSMessagesSent;
                Items.Sort(new OrderSMSDesc());
                foreach (SMSStorage item in Items)
                {
                    UCSMS UC = new UCSMS();
                    UC.IndexSMS = item.ID;
                    UC.TypeMsg = item.Location;
                    UC.Message = item.Message;
                    UC.Number = item.Number;
                    UC.NamePhonebook = FindName(item.Number);
                    UC.Timezone = item.TimeZone;
                    UC.DateTime = item.DateTime;
                    UC.EventUCSMSChecked += new UCSMS.UCSMSCheckedEventHandler(UC_EventUCSMSChecked);
                    UC.EventUCSMSSeleted += new UCSMS.UCSMSSelectedEventHandler(UC_EventUCSMSSeleted);
                    flowLayoutOutBox.Controls.Add(UC);
                }
            }
            TabSent.Text = ResourcesText.WordOutbox + " (" + Items.Count.ToString().PadLeft(2, '0') + ")";
        }
        private void TxtNumber_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\b')
            {
                e.Handled = false;
                return;
            }
            else if (e.KeyChar == ';')
            {
                e.Handled = false;
                return;
            }
            else if (e.KeyChar == '#')
            {
                e.Handled = false;
                return;
            }
            else if (e.KeyChar == '*')
            {
                e.Handled = false;
                return;
            }
            else if ((e.KeyChar < '0') || (e.KeyChar > '9'))
            {
                e.Handled = true;
            }
        }

        private void ChkListPhoneBook_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            bool mark = (e.NewValue == CheckState.Checked);
            if (mark)
            {
                string code = Properties.Settings.Default.DefaultCodeOper.ToUpper().Replace("X", "");
                string num = ((PhoneBook)ChkListPhoneBook.SelectedItem).Number;
                if (num.StartsWith(code))
                {
                    num = num.Substring(Properties.Settings.Default.DefaultCodeOper.Length);
                }
                if (TxtNumber.Text == "")
                {
                    TxtNumber.Text = num;
                }
                else
                {
                    TxtNumber.Text += ";" + num;
                }
            }
            else
            {
                string[] numbers = TxtNumber.Text.Split(';');
                string delnum = ((PhoneBook)ChkListPhoneBook.SelectedItem).Number;
                string newnumber = "";
                foreach (string item in numbers)
                {
                    if (item != delnum)
                    {
                        string code = Properties.Settings.Default.DefaultCodeOper.ToUpper().Replace("X", "");
                        if (delnum.StartsWith(code))
                        {
                            delnum = delnum.Substring(Properties.Settings.Default.DefaultCodeOper.Length);
                        }
                        if (item != delnum)
                        {
                            newnumber += item + ";";
                        }
                    }
                }
                if (newnumber.EndsWith(";"))
                {
                    newnumber = newnumber.Substring(0, newnumber.Length - 1);
                }
                TxtNumber.Text = newnumber;
            }
            TxtNumber.SelectionStart = TxtNumber.Text.Length;
            TxtNumber.ScrollToCaret();
            if (!mark)
            {
                TxtNumber.ReadOnly = (ChkListPhoneBook.CheckedIndices.Count - 1 > 0);
            }
            else
            {
                TxtNumber.ReadOnly = true;
            }
        }
        private void TxtMsg_TextChanged(object sender, EventArgs e)
        {
            if (TxtMsg.Text.Length > 160)
            {
                TxtMsg.Text = TxtMsg.Text.Substring(0, 160);
            }
            LblQtdMsg.Text = TxtMsg.Text.Length.ToString() +  "/160";
            if (TxtMsg.Text.Length > 0 && TxtNumber.Text.Length > 0)
            {
                BntSendSMS.ForeColor = Color.White;
            }
            else
            {
                BntSendSMS.ForeColor = Color.LightGray;
            }
        }

        private void TxtNumber_TextChanged(object sender, EventArgs e)
        {
            if (TxtMsg.Text.Length > 0 && TxtNumber.Text.Length > 0)
            {
                BntSendSMS.ForeColor = Color.White;
            }
            else
            {
                BntSendSMS.ForeColor = Color.LightGray;
            }
            if (TxtNumber.Text == "")
            {
                foreach (int pos in ChkListPhoneBook.CheckedIndices)
                {
                    ChkListPhoneBook.SetItemChecked(pos, false);
                }
            }
            TxtNumber.ReadOnly = (ChkListPhoneBook.CheckedIndices.Count > 0);
        }

        private void TxtMsg_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\b')
            {
                e.Handled = false;
                return;
            }
            else if (e.KeyChar == '\r')
            {
                e.Handled = false;
                return;
            }
            else if (!" !\"#$%&'()*+,-./0123456789:;<=>?@abcdefghijlkmnopqrstuvxwyz[\\]^-`{|}~".Contains(e.KeyChar.ToString().ToLower()))
            {
                e.Handled = true;
                return;
            }
        }

        private void TxtMsg_Leave(object sender, EventArgs e)
        {
            TxtMsg.Text = TxtMsg.Text.TrimEnd();
            while (TxtMsg.Text.StartsWith("\r"))
            {
                TxtMsg.Text = TxtMsg.Text.Substring(1);
            }
            while (TxtMsg.Text.StartsWith("\n"))
            {
                TxtMsg.Text = TxtMsg.Text.Substring(1);
            }
            while (TxtMsg.Text.StartsWith("\r"))
            {
                TxtMsg.Text = TxtMsg.Text.Substring(0, TxtMsg.Text.Length - 1);
            }
            while (TxtMsg.Text.StartsWith("\n"))
            {
                TxtMsg.Text = TxtMsg.Text.Substring(0, TxtMsg.Text.Length - 1);
            }
        }

        private void BntSendSMS_Click(object sender, EventArgs e)
        {
            if (TxtMsg.Text.Length <= 0 || TxtNumber.Text.Length <= 0)
            {
                SharedLib.MsgAlert(ResourcesText.MsgandNumberRequired); 
                return;
            }
            SharedLib.MainForm.timerStatus.Stop();
            SharedLib.StartWaitProcessThread();
            SharedLib.EndWaitProcessThread();

            this.Cursor = Cursors.WaitCursor;
            TxtMsg.Enabled = false;
            TxtNumber.Enabled = false;
            BntSendSMS.Enabled = false;
            BntClear.Enabled = false;
            BntCancel.Enabled = false;

            bool savread = BntRead.Enabled;
            bool savcopy = BntEdt.Enabled;
            bool savdel = BntDelete.Enabled;

            BntRead.Enabled = false;
            BntEdt.Enabled = false;
            BntDelete.Enabled = false;
            BntTrack.Enabled = false;
            
            Application.DoEvents();
            string[] Numbers = TxtNumber.Text.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            if (ChkSaveOutBox.Checked)
            {
                TabInOutBox.SelectedIndex = 2;
            }
            int QtdNotSend = 0;
            string MsgErr = "";
            string CharSet = "";
            List<string> NumsOk = new List<string>();
            if (SharedLib.ModemGSM.ModelATCommands.CharSetsCMD)
            {
                CharSet = Properties.Settings.Default.DefaultSMSCharSet;
            }
            for (int i = 0; i < Numbers.Length; i++)
            {
                SharedLib.ModemGSM.KeepAlive();
                int posSentSMS = -1;
                Application.DoEvents();
                Thread.Sleep(1000);
                Application.DoEvents();
                StatusSMSReport sr = new StatusSMSReport();
                if (SharedLib.ModemGSM.Online)
                {
                    if (SharedLib.ModemGSM.ModelATCommands.SMSConfigIndicationsCMD)
                    {
                        sr.DateTimeReceiver = null;
                        sr.DateTimeSend = System.DateTime.Now;
                        sr.MessageSend = TxtMsg.Text;
                        sr.Number = Numbers[i];
                        sr.Status = -1;
                        if (ChkSaveOutBox.Checked)
                        {
                            sr.Storage = SharedLib.ModemGSM.SMSStorages.WriteSend;
                        }
                        else
                        {
                            sr.Storage = "";
                        }

                        sr.TimezoneReceiver = null;
                        sr.TimezoneSend = System.TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now);
                        sr.TypeCode = "";
                        sr.TypeReport = TypeStatusReportEnum.SendConfirmation;
                    }
                    posSentSMS = SharedLib.ModemGSM.SMSSendTextFormat(Numbers[i], TxtMsg.Text, ChkSaveOutBox.Checked, CharSet);
                    if (SharedLib.ModemGSM.ModelATCommands.SMSConfigIndicationsCMD)
                    {
                        if (ChkSaveOutBox.Checked)
                        {
                            sr.IndexStorage = posSentSMS;
                        }
                        else
                        {
                            sr.IndexStorage = -1;
                        }
                    }
                }
                if (posSentSMS < 0)
                {
                    MsgErr += "\t" + Numbers[i] + "\r\n";
                    QtdNotSend++;
                }
                else
                {
                    if (SharedLib.ModemGSM.ModelATCommands.SMSConfigIndicationsCMD)
                    {
                        SharedLib.ModemGSM.SMSAddStatusReport(sr);
                    }
                    Application.DoEvents();
                    NumsOk.Add(Numbers[i]);
                }
            }
            if (SharedLib.ModemGSM.ModelATCommands.CharSetsCMD)
            {
                SharedLib.ModemGSM.CharSetWrite(Properties.Settings.Default.DefaultCharSet);
            }
            this.Cursor = Cursors.WaitCursor;
            if (NumsOk.Count > 0)
            {
                if (SharedLib.ModemGSM.ModelATCommands.SMSConfigIndicationsCMD)
                {
                    BntTrack.Enabled = true;
                }
            }
            else
            {
                BntTrack.Enabled = false;
            }
            foreach (string numsend in NumsOk)
            {
                TxtNumber.Text = TxtNumber.Text.Replace(numsend, "");
                TxtNumber.Text = TxtNumber.Text.Replace(";;", ";");
                int Positem = -1;
                foreach (int pos in ChkListPhoneBook.CheckedIndices)
                {
                    PhoneBook item = (PhoneBook)ChkListPhoneBook.Items[pos];
                    if (numsend.Length > item.Number.Length)
                    {
                        if (numsend.Contains(item.Number))
                        {
                            Positem = pos;
                            break;
                        }
                    }
                    else if (numsend.Length == item.Number.Length)
                    {
                        if (numsend == item.Number)
                        {
                            Positem = pos;
                            break;
                        }
                    }
                    else if (numsend.Length < item.Number.Length)
                    {
                        if (item.Number.Contains(numsend))
                        {
                            Positem = pos;
                            break;
                        }
                    }
                    item = null;
                }
                if (Positem >=0)
                {
                    ChkListPhoneBook.SetItemChecked(Positem, false);
                }
            }
            this.Cursor = Cursors.WaitCursor;
            if (SharedLib.ModemGSM.ModelATCommands.SMSListMessageCMD)
            {
                SharedLib.ModemGSM.SMSListMessagesTextFormat();
            }
            SharedLib.QtdNewSMSMessage = 0;
            ShowInBox();
            ShowUnSent();
            ShowSent();
            SharedLib.MainForm.ShowSMSMessageCount();
            BntRead.Enabled = savread;
            BntEdt.Enabled = savcopy;
            BntDelete.Enabled = savdel;
            TxtMsg.Enabled = true;
            TxtNumber.Enabled = true;
            BntSendSMS.Enabled = true;
            BntClear.Enabled = true;
            BntCancel.Enabled = true;
            SharedLib.MainForm.timerStatus.Start();
            this.Cursor = Cursors.Default;
            if (QtdNotSend != 0)
            {
                SharedLib.MsgErro(ResourcesText.ErrSMSSent + " : \r\n" + MsgErr);
            }
            else
            {
                if (SharedLib.ModemGSM.ModelATCommands.SMSConfigIndicationsCMD)
                {
                    SharedLib.MsgAlert(ResourcesText.SMSTrackOK);
                }
                else
                {
                    SharedLib.MsgAlert(ResourcesText.SMSSendOK);
                }
            }
            if (NumsOk.Count == Numbers.Length)
            {
                if (SharedLib.ModemGSM.ModelATCommands.SMSConfigIndicationsCMD)
                {
                    BntTrack.PerformClick();
                }
            }
        }

        private void lnkUnPhonebook_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            foreach (int pos in ChkListPhoneBook.CheckedIndices)
            {
                ChkListPhoneBook.SetItemChecked(pos, false);
                string[] numbers = TxtNumber.Text.Split(';');
                string delnum = ((PhoneBook)ChkListPhoneBook.Items[pos]).Number;
                string newnumber = "";
                foreach (string item in numbers)
                {
                    if (item != delnum)
                    {
                        string code = Properties.Settings.Default.DefaultCodeOper.ToUpper().Replace("X", "");
                        if (delnum.StartsWith(code))
                        {
                            delnum = delnum.Substring(Properties.Settings.Default.DefaultCodeOper.Length);
                        }
                        if (item != delnum)
                        {
                            newnumber += item + ";";
                        }
                    }
                }
                if (newnumber.EndsWith(";"))
                {
                    newnumber = newnumber.Substring(0, newnumber.Length - 1);
                }
                TxtNumber.Text = newnumber;
            }
            TxtNumber.SelectionStart = TxtNumber.Text.Length;
            TxtNumber.ScrollToCaret();
        }

        private void lnkAllBox_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            MarkUnMarkPanels(true);
        }

        private void lnkUnAllBox_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            MarkUnMarkPanels(false);
        }
        private void MarkUnMarkPanels(bool value)
        {
            Control.ControlCollection CtrlCols = FindCurrentPanel();
            foreach (UCSMS item in CtrlCols)
            {
                item.Checked = value;
            }
            CountCheckedPanels(CtrlCols);
        }
        private Control.ControlCollection FindCurrentPanel()
        {
            string PnlName = TabInOutBox.SelectedTab.Name;
            Control.ControlCollection CtrlCols = null;
            if (PnlName == "TabUnSent")
            {
                CtrlCols = flowLayoutOutUnSent.Controls;
            }
            else if (PnlName == "TabInBox")
            {
                CtrlCols = flowLayoutInBox.Controls;
            }
            else if (PnlName == "TabSent")
            {
                CtrlCols = flowLayoutOutBox.Controls;
            }
            return CtrlCols;
        }
        private void CountCheckedPanels(Control.ControlCollection Ctrls)
        {
            int qtd = 0;
            if (Ctrls != null)
            {
                foreach (UCSMS item in Ctrls)
                {
                    if (item.Checked)
                    {
                        qtd++;
                    }
                }
            }
            if (SharedLib.ModemGSM.ModelATCommands.SMSDeleteMessageStorageCMD)
            {
                BntDelete.Enabled = (qtd > 0);
            }
            else
            {
                BntDelete.Enabled = false;
                lnkUnAllBox.Enabled = false;
                lnkAllBox.Enabled = false;
            }
        }
        private void TabInOutBox_Selected(object sender, TabControlEventArgs e)
        {
            Control.ControlCollection CtrlCols = FindCurrentPanel();
            CountCheckedPanels(CtrlCols);
            BntEdt.Enabled = false;
            BntRead.Enabled = false;
        }

        private void BntRead_Click(object sender, EventArgs e)
        {
            FrmReadSMS frm = new FrmReadSMS();
            frm.Owner = this;
            frm.Number = UCSelected.Number;
            frm.NamePhonebook = UCSelected.NamePhonebook;
            frm.DateTime = UCSelected.DateTime;
            frm.TimeZone = UCSelected.Timezone;
            frm.Message = UCSelected.Message;
            switch (UCSelected.TypeMsg)
            {
                case LocationSMS.All:
                    frm.LocationBox = "";
                    break;
                case LocationSMS.UnRead:
                    frm.LocationBox = ResourcesText.WordInbox;
                    break;
                case LocationSMS.Read:
                    frm.LocationBox = ResourcesText.WordInbox;
                    break;
                case LocationSMS.Unsent:
                    frm.LocationBox = ResourcesText.Unsent;
                    break;
                case LocationSMS.Sent:
                    frm.LocationBox = ResourcesText.WordOutbox;
                    break;
            }
            this.Opacity = 0.30;
            frm.TopMost = true;
            frm.ShowDialog();
            UCSelected.Focus();
            UCSelected = null;
            this.Opacity = 1;
        }

        private void BntEdt_Click(object sender, EventArgs e)
        {
            TxtMsg.Text = UCSelected.Message;
            if (TxtNumber.Text == "")
            {
                if (UCSelected.Number.Length > (Properties.Settings.Default.DefaultLengthCelPhone + Properties.Settings.Default.DefaultLengthCodeDDD))
                {
                    int dif = UCSelected.Number.Length - (Properties.Settings.Default.DefaultLengthCelPhone + Properties.Settings.Default.DefaultLengthCodeDDD);
                    TxtNumber.Text =  UCSelected.Number.Substring(dif);
                }
                else
                {
                    TxtNumber.Text =  UCSelected.Number;
                }
            }
            UCSelected.Focus();
            UCSelected = null;
        }

        private void BntDelete_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;

            SharedLib.MainForm.timerStatus.Stop();
            SharedLib.StartWaitProcessThread();
            SharedLib.EndWaitProcessThread();

            Control.ControlCollection CtrlCols = FindCurrentPanel();
            bool OK = false;
            for (int i = 0; i < CtrlCols.Count; i++)
            {
                if (((UCSMS)CtrlCols[i]).Checked)
                {
                    OK = SharedLib.ModemGSM.SMSDeleteMessage(((UCSMS)CtrlCols[i]).IndexSMS);
                    if (!OK)
                    {
                        SharedLib.MsgErro(ResourcesText.ErrSMSDEL + " = " + ((UCSMS)CtrlCols[i]).IndexSMS);
                        break;
                    }
                    else
                    {
                        CtrlCols.RemoveAt(i);
                        i--;
                    }
                }
            }
            if (SharedLib.ModemGSM.ModelATCommands.SMSListMessageCMD)
            {
                SharedLib.ModemGSM.SMSListMessagesTextFormat();
            }
            SharedLib.QtdNewSMSMessage = 0;
            ShowInBox();
            ShowUnSent();
            ShowSent();
            SharedLib.MainForm.ShowSMSMessageCount();
            SharedLib.MainForm.timerStatus.Start();
            this.Cursor = Cursors.Default;
        }

        private void BntClear_Click(object sender, EventArgs e)
        {
            if (SharedLib.ModemGSM.ModelATCommands.SMSListMessageCMD)
            {
                SharedLib.ModemGSM.SMSListMessagesTextFormat();
            }
            SharedLib.QtdNewSMSMessage = 0;
            TxtNumber.Text = "";
            TxtMsg.Text = "";
            BntTrack.Enabled = false;
            foreach (int pos in ChkListPhoneBook.CheckedIndices)
            {
                ChkListPhoneBook.SetItemChecked(pos, false);                
            }
        }

        private void BntTrack_Click(object sender, EventArgs e)
        {
            this.Opacity = 0.3;
            SharedLib.ModemGSM.SMSListMessagesTextFormat();
            SharedLib.QtdNewSMSMessage = 0;
            SharedLib.StartWaitProcessThread();
            SharedLib.EndWaitProcessThread();
            FrmReport frm = new FrmReport();
            frm.Owner = this;
            frm.ShowDialog();
            if (SharedLib.ModemGSM.KeepAlive())
            {
                string FileName = SharedLib.CreateNameReport(SharedLib.ModemGSM.IMEI);
                SharedLib.SaveReportFileName(SharedLib.ModemGSM.SMSStatusReport, FileName);
                SharedLib.SaveReport = false;
            }
            else
            {
                SharedLib.SaveReport = true;
            }
            this.Opacity = 1;
        }
    }
}
